﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using _MetalSlug.Soldiers.Units;
using _MetalSlug.Weapons.Units;
using Microsoft.Xna.Framework;

namespace _MetalSlug.Mementoes
{
    public class SoldierMemento : IMemento
    {
        int id;
        public int Id
        {
            get { return id; }
        }

        int hp;
        public int Hp
        {
            get { return hp; }
        }
        Vector2 topLeft;
        public Vector2 TopLeft
        {
            get { return topLeft; }
        }

        Enemy.CURRENT_ACTIVITY currentActivity;
        public Enemy.CURRENT_ACTIVITY CurrentActivity
        {
            get { return currentActivity; }
        }

        Enemy.RESOURCE_FACE currentFacing;
        public Enemy.RESOURCE_FACE CurrentFacing
        {
            get { return currentFacing; }
        }

        int indexTexture;

        public int IndexTexture
        {
            get { return indexTexture; }
        }

        List<BulletMemento> lstBullet;

        public SoldierMemento(Enemy RepresentEnemy)
        {
            id = RepresentEnemy.Id;
            hp = RepresentEnemy.Hp;
            topLeft = RepresentEnemy.TopLeft;
            currentActivity = RepresentEnemy.Activity;
            currentFacing = RepresentEnemy.Facing;
            indexTexture = RepresentEnemy.IndexCurrent;

            lstBullet = new List<BulletMemento>();
            foreach(Bullet bullet in RepresentEnemy.Bullets)
            {
                BulletMemento bulletMemento = new BulletMemento(bullet);
                lstBullet.Add(bulletMemento);
            }
        }

        public SoldierMemento(XmlElement Element)
        {
            id = int.Parse(Element.GetAttribute("ID"));
            hp = int.Parse(Element.GetAttribute("Hp"));
            currentActivity = (Enemy.CURRENT_ACTIVITY)Enum.Parse(typeof(Enemy.CURRENT_ACTIVITY), Element.GetAttribute("Activity"));
            currentFacing = (Enemy.RESOURCE_FACE) Enum.Parse(typeof(Enemy.RESOURCE_FACE), Element.GetAttribute("Facing"));
            
            float X, Y;
            X = float.Parse(Element.GetAttribute("X"));
            Y = float.Parse(Element.GetAttribute("Y"));
            topLeft = new Vector2(X, Y);

            lstBullet = new List<BulletMemento>();
            foreach (XmlElement Child in Element.ChildNodes)
            {
                BulletMemento bullet = new BulletMemento(Child);
                lstBullet.Add(bullet);
            }
        }

        public XmlElement ToXML(XmlDocument Doc)
        {
            XmlElement Element = Doc.CreateElement("Soldier");
            Element.SetAttribute("ID", Id.ToString());
            Element.SetAttribute("Hp", Hp.ToString());
            Element.SetAttribute("Activity", CurrentActivity.ToString());
            Element.SetAttribute("Facing", CurrentFacing.ToString());
            Element.SetAttribute("X", TopLeft.X.ToString());
            Element.SetAttribute("Y", TopLeft.Y.ToString());
            foreach(BulletMemento Bullet in lstBullet)
            {
                Element.AppendChild(Bullet.ToXML(Doc));
            }

            return Element;
        }
    }
}
